<template>
	<view class="wrap">
		<filters :subName="subName" :gradeName="gradeName" @selectCurrSub="selectCurrSub" @selectCurrGrade="selectCurrGrade"/>
		<view class="bbsList">
			<template v-if="isHasDataFlag">
				<bbs-list :bbsList="bbsList" @supportOrNot="supportOrNot" @goFavor="goFavor" @goBbsDet="goBbsDet"></bbs-list>
				<loading-view :loading="loading"></loading-view>
			</template>
			<template v-else-if="isHasDataFlag == false">
				<no-data></no-data>
			</template>
		</view>
		<view class="botPart" @tap="goPubBbs">
			<text class="iconfont icon-bianji1"></text>
			<text>我要发帖</text>
		</view>
	</view>
</template>

<script>
	import Filters from './components/Filters'
	import BbsList from './components/BbsList'
	import { getBbsList,supportMotify,collectMotify } from '@/api/zhuxue'
	export default {
		components:{
			Filters,
			BbsList
		},
		data() {
			return {
				subName:'全部',
				gradeName:'全部',
				subId:0,
				nowPage:1,
				loading:false,
				isHasDataFlag:null,
				bbsList:[],
				isRefreshFlag:false,
				gradeNo:0
			}
		},
		onLoad(options){
			this.gradeNo = options.gradeNo;
			this.loadBbsList();
		},
		onPullDownRefresh : function(){
			this.initParams();
			uni.stopPullDownRefresh();
		},
		onReachBottom : function(){
			if( !this.loading ){
				this.loadBbsList();
			}
		},
		onShow(){
			if(this.isRefreshFlag){
				this.initParams();
			}
		},
		onHide(){
			this.isRefreshFlag = false;
		},
		methods: {
			onNavigationBarButtonTap(e){
				if(e.index == 0){
					uni.navigateTo({
						url:'/pages/subIndex/bbsCenter/myBbs'
					})
				}
			},
			goBbsDet(id){
				uni.navigateTo({
					url:'/pages/subIndex/bbsCenter/bbsDet?detId=' + id
				})
			},
			supportOrNot(index,id,support){
				var field = {  account:uni.getStorageSync('token'),id,support };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				var tmpBbs = this.bbsList[index];
				supportMotify({content:contentStr}).then(res=>{
					if(support == 0){
						tmpBbs.unSupport += 1;
						tmpBbs.unSuppIndex = index;
					}else{
						tmpBbs.support += 1;
						tmpBbs.suppIndex = index;
					}
					tmpBbs.isSuppFlag = res.data.datas.flag;
				}).catch(err=>{
					console.log(err)
				});
			},
			goFavor(index,id){
				var field = { account:uni.getStorageSync('token'),id };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				collectMotify({content:contentStr}).then(res=>{
					var tmpBbs = this.bbsList[index];
					if(res.data.datas.collectTag == 1){
						tmpBbs.concern += 1;
					}else{
						tmpBbs.concern -= 1;
					}
				}).catch(err=>{
					console.log(err)
				});
			},
			goPubBbs(){
				uni.navigateTo({
					url:'/pages/subIndex/bbsCenter/pubBbs?gradeNo=' + this.gradeNo
				})
			},
			loadBbsList(){
				let { nowPage,bbsList } = this;
				this.loading = true;
				var field = { subject:this.subId,gread:this.gradeName,pageNo:this.nowPage,pageSize:30 };
				var contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getBbsList({ content:contentStr }).then(res=>{
					if(this.nowPage == 1){
						if(res.data.datas.dlist.length == 0){
							this.loading = false;
							this.isHasDataFlag=false;
						}
					}
					if(res.data.datas.dlist.length > 0){
						nowPage += 1;
						bbsList.push( ...res.data.datas.dlist );
						this.bbsList = bbsList;
						this.nowPage = nowPage;
						this.loading = false;
						this.isHasDataFlag=true;
					}else{
						if(this.nowPage > 1){
							this.loading = false;
						}
					} 
				}).catch(err=>{
					console.log(err)
					this.loading = false;
					this.isHasDataFlag=false;
				});
			},
			initParams(){
				this.nowPage = 1;
				this.loading = false;
				this.bbsList = [];
				this.loadBbsList();
			},
			selectCurrSub(id,subName){
				this.subId = id;
				this.subName = subName;
				this.initParams();
			},
			selectCurrGrade(gradeName){
				this.gradeName = gradeName;
				this.initParams();
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $pss-bg-primary;
		@include respTo(phone){
			margin-top: 110rpx;
		}
		@include respTo(pad){
			margin-top: 70rpx;
		}
	}
</style>
<style lang="scss" scoped>
	.botPart{
		@include respTo(phone){
			height: 100rpx;
			font-size: $font-size30;
		}
		@include respTo(pad){
			height: 60rpx;
			font-size: $padSize18;
		}
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		background: $pss-color-blue;
		position: fixed;
		left: 0;
		bottom: 0rpx;
		z-index: 100; 
		color: #fff;
		.icon-bianji1{
			margin-right: 10rpx;
			font-size: 36rpx;
		}
	}
	.wrap{
		@include respTo(phone){
			padding-bottom: 100rpx;
		}
		@include respTo(pad){
			padding-bottom: 60rpx;
		}
	}
</style>
